import { axios } from '@/utils/request'

const api = {
  // 半成品销售订单主表
  semiProductSalesOrder: '/oms/semiProductSalesOrder',
  // 半成品销售订单明细
  semiProductSalesOrderDetail: '/oms/semiProductSalesOrderDetail',
  // 半成品销售订单交易记录
  semiProductSalesOrderTransaction: '/oms/semiProductSalesOrderTransaction',
  // 半成品销售订单进度
  semiProductSalesOrderProgress: '/oms/semiProductSalesOrderProgress'
}

// ==================== 半成品销售订单主表 API ====================

/**
 * 分页查询半成品销售订单列表
 */
export function getSemiProductSalesOrderList(parameter) {
  return axios({
    url: api.semiProductSalesOrder + '/list',
    method: 'get',
    params: parameter
  })
}

/**
 * 根据ID查询半成品销售订单
 */
export function getSemiProductSalesOrderById(id) {
  return axios({
    url: api.semiProductSalesOrder + '/queryById',
    method: 'get',
    params: { id }
  })
}

/**
 * 新增半成品销售订单
 */
export function addSemiProductSalesOrder(data) {
  return axios({
    url: api.semiProductSalesOrder + '/add',
    method: 'post',
    data: data
  })
}

/**
 * 编辑半成品销售订单
 */
export function editSemiProductSalesOrder(data) {
  return axios({
    url: api.semiProductSalesOrder + '/edit',
    method: 'put',
    data: data
  })
}

/**
 * 删除半成品销售订单
 */
export function deleteSemiProductSalesOrder(id) {
  return axios({
    url: api.semiProductSalesOrder + '/delete',
    method: 'delete',
    params: { id }
  })
}

/**
 * 批量删除半成品销售订单
 */
export function deleteBatchSemiProductSalesOrder(ids) {
  return axios({
    url: api.semiProductSalesOrder + '/deleteBatch',
    method: 'delete',
    params: { ids }
  })
}

/**
 * 审核半成品销售订单
 */
export function auditSemiProductSalesOrder(id, auditStatus, auditComment) {
  return axios({
    url: api.semiProductSalesOrder + '/audit',
    method: 'post',
    params: { id, status: auditStatus, auditRemark: auditComment }
  })
}

/**
 * 提交审核半成品销售订单
 */
export function submitSemiProductSalesOrder(id) {
  return axios({
    url: api.semiProductSalesOrder + '/submit',
    method: 'post',
    params: { id }
  })
}

/**
 * 取消半成品销售订单
 */
export function cancelSemiProductSalesOrder(id, cancelReason) {
  return axios({
    url: api.semiProductSalesOrder + '/cancel',
    method: 'post',
    params: { id, cancelReason }
  })
}

/**
 * 完成半成品销售订单
 */
export function completeSemiProductSalesOrder(id) {
  return axios({
    url: api.semiProductSalesOrder + '/complete',
    method: 'post',
    params: { id }
  })
}

/**
 * 根据客户ID查询订单
 */
export function getSemiProductSalesOrderByCustomerId(customerId) {
  return axios({
    url: api.semiProductSalesOrder + '/getByCustomerId',
    method: 'get',
    params: { customerId }
  })
}

/**
 * 根据项目ID查询订单
 */
export function getSemiProductSalesOrderByProjectId(projectId) {
  return axios({
    url: api.semiProductSalesOrder + '/getByProjectId',
    method: 'get',
    params: { projectId }
  })
}

/**
 * 根据状态查询订单
 */
export function getSemiProductSalesOrderByStatus(status) {
  return axios({
    url: api.semiProductSalesOrder + '/getByStatus',
    method: 'get',
    params: { status }
  })
}

/**
 * 计算订单总金额
 */
export function calculateOrderTotalAmount(orderId) {
  return axios({
    url: api.semiProductSalesOrder + '/calculateTotalAmount',
    method: 'get',
    params: { orderId }
  })
}

/**
 * 导出Excel
 */
export function exportSemiProductSalesOrderExcel(parameter) {
  return axios({
    url: api.semiProductSalesOrder + '/exportXls',
    method: 'get',
    params: parameter,
    responseType: 'blob'
  })
}

// ==================== 半成品销售订单明细 API ====================

/**
 * 分页查询半成品销售订单明细列表
 */
export function getSemiProductSalesOrderDetailList(parameter) {
  return axios({
    url: api.semiProductSalesOrderDetail + '/list',
    method: 'get',
    params: parameter
  })
}

/**
 * 根据订单ID查询明细
 */
export function getSemiProductSalesOrderDetailByOrderId(orderId) {
  return axios({
    url: api.semiProductSalesOrderDetail + '/getByOrderId',
    method: 'get',
    params: { orderId }
  })
}

/**
 * 新增半成品销售订单明细
 */
export function addSemiProductSalesOrderDetail(data) {
  return axios({
    url: api.semiProductSalesOrderDetail + '/add',
    method: 'post',
    data: data
  })
}

/**
 * 批量保存订单明细
 */
export function saveSemiProductSalesOrderDetailBatch(orderId, detailList) {
  return axios({
    url: api.semiProductSalesOrderDetail + '/saveBatch',
    method: 'post',
    params: { orderId },
    data: detailList
  })
}

/**
 * 编辑半成品销售订单明细
 */
export function editSemiProductSalesOrderDetail(data) {
  return axios({
    url: api.semiProductSalesOrderDetail + '/edit',
    method: 'put',
    data: data
  })
}

/**
 * 删除半成品销售订单明细
 */
export function deleteSemiProductSalesOrderDetail(id) {
  return axios({
    url: api.semiProductSalesOrderDetail + '/delete',
    method: 'delete',
    params: { id }
  })
}

/**
 * 根据物料ID查询明细
 */
export function getSemiProductSalesOrderDetailByMaterialId(materialId) {
  return axios({
    url: api.semiProductSalesOrderDetail + '/getByMaterialId',
    method: 'get',
    params: { materialId }
  })
}

/**
 * 计算明细小计金额
 */
export function calculateDetailSubtotal(detailId) {
  return axios({
    url: api.semiProductSalesOrderDetail + '/calculateSubtotal',
    method: 'get',
    params: { detailId }
  })
}

// ==================== 半成品销售订单交易记录 API ====================

/**
 * 分页查询半成品销售订单交易记录列表
 */
export function getSemiProductSalesOrderTransactionList(parameter) {
  return axios({
    url: api.semiProductSalesOrderTransaction + '/list',
    method: 'get',
    params: parameter
  })
}

/**
 * 根据订单ID查询交易记录
 */
export function getSemiProductSalesOrderTransactionByOrderId(orderId) {
  return axios({
    url: api.semiProductSalesOrderTransaction + '/getByOrderId',
    method: 'get',
    params: { orderId }
  })
}

/**
 * 新增半成品销售订单交易记录
 */
export function addSemiProductSalesOrderTransaction(data) {
  return axios({
    url: api.semiProductSalesOrderTransaction + '/add',
    method: 'post',
    data: data
  })
}

/**
 * 编辑半成品销售订单交易记录
 */
export function editSemiProductSalesOrderTransaction(data) {
  return axios({
    url: api.semiProductSalesOrderTransaction + '/edit',
    method: 'put',
    data: data
  })
}

/**
 * 删除半成品销售订单交易记录
 */
export function deleteSemiProductSalesOrderTransaction(id) {
  return axios({
    url: api.semiProductSalesOrderTransaction + '/delete',
    method: 'delete',
    params: { id }
  })
}

/**
 * 处理付款
 */
export function processPayment(orderId, amount, transactionType, paymentMethod, description, voucher) {
  return axios({
    url: api.semiProductSalesOrderTransaction + '/processPayment',
    method: 'post',
    params: { orderId, amount, transactionType, paymentMethod, description, voucher }
  })
}

/**
 * 处理退款
 */
export function processRefund(orderId, amount, description, reason) {
  return axios({
    url: api.semiProductSalesOrderTransaction + '/processRefund',
    method: 'post',
    params: { orderId, amount, description, reason }
  })
}

/**
 * 计算订单已付金额
 */
export function calculatePaidAmount(orderId) {
  return axios({
    url: api.semiProductSalesOrderTransaction + '/calculatePaidAmount',
    method: 'get',
    params: { orderId }
  })
}

/**
 * 计算订单预付款金额
 */
export function calculatePrepaymentAmount(orderId) {
  return axios({
    url: api.semiProductSalesOrderTransaction + '/calculatePrepaymentAmount',
    method: 'get',
    params: { orderId }
  })
}

/**
 * 根据交易类型查询记录
 */
export function getSemiProductSalesOrderTransactionByType(orderId, transactionType) {
  return axios({
    url: api.semiProductSalesOrderTransaction + '/getByTransactionType',
    method: 'get',
    params: { orderId, transactionType }
  })
}

// ==================== 半成品销售订单进度 API ====================

/**
 * 分页查询半成品销售订单进度列表
 */
export function getSemiProductSalesOrderProgressList(parameter) {
  return axios({
    url: api.semiProductSalesOrderProgress + '/list',
    method: 'get',
    params: parameter
  })
}

/**
 * 根据订单ID查询进度记录
 */
export function getSemiProductSalesOrderProgressByOrderId(orderId) {
  return axios({
    url: api.semiProductSalesOrderProgress + '/getByOrderId',
    method: 'get',
    params: { orderId }
  })
}

/**
 * 新增半成品销售订单进度记录
 */
export function addSemiProductSalesOrderProgress(data) {
  return axios({
    url: api.semiProductSalesOrderProgress + '/add',
    method: 'post',
    data: data
  })
}

/**
 * 编辑半成品销售订单进度记录
 */
export function editSemiProductSalesOrderProgress(data) {
  return axios({
    url: api.semiProductSalesOrderProgress + '/edit',
    method: 'put',
    data: data
  })
}

/**
 * 删除半成品销售订单进度记录
 */
export function deleteSemiProductSalesOrderProgress(id) {
  return axios({
    url: api.semiProductSalesOrderProgress + '/delete',
    method: 'delete',
    params: { id }
  })
}

/**
 * 记录状态变更
 */
export function recordStatusChange(orderId, oldStatus, newStatus, operationType, operationContent, changeReason, remark) {
  return axios({
    url: api.semiProductSalesOrderProgress + '/recordStatusChange',
    method: 'post',
    params: { orderId, oldStatus, newStatus, operationType, operationContent, changeReason, remark }
  })
}

/**
 * 根据状态查询进度记录
 */
export function getSemiProductSalesOrderProgressByStatus(orderId, status) {
  return axios({
    url: api.semiProductSalesOrderProgress + '/getByStatus',
    method: 'get',
    params: { orderId, status }
  })
}

/**
 * 根据操作类型查询进度记录
 */
export function getSemiProductSalesOrderProgressByOperationType(operationType) {
  return axios({
    url: api.semiProductSalesOrderProgress + '/getByOperationType',
    method: 'get',
    params: { operationType }
  })
}

/**
 * 获取订单最新进度
 */
export function getLatestProgress(orderId) {
  return axios({
    url: api.semiProductSalesOrderProgress + '/getLatestProgress',
    method: 'get',
    params: { orderId }
  })
}

/**
 * 批量添加进度记录
 */
export function batchAddProgress(progressList) {
  return axios({
    url: api.semiProductSalesOrderProgress + '/batchAdd',
    method: 'post',
    data: progressList
  })
}

export default {
  // 主表API
  getSemiProductSalesOrderList,
  getSemiProductSalesOrderById,
  addSemiProductSalesOrder,
  editSemiProductSalesOrder,
  deleteSemiProductSalesOrder,
  deleteBatchSemiProductSalesOrder,
  auditSemiProductSalesOrder,
  submitSemiProductSalesOrder,
  cancelSemiProductSalesOrder,
  completeSemiProductSalesOrder,
  getSemiProductSalesOrderByCustomerId,
  getSemiProductSalesOrderByProjectId,
  getSemiProductSalesOrderByStatus,
  calculateOrderTotalAmount,
  exportSemiProductSalesOrderExcel,
  
  // 明细API
  getSemiProductSalesOrderDetailList,
  getSemiProductSalesOrderDetailByOrderId,
  addSemiProductSalesOrderDetail,
  saveSemiProductSalesOrderDetailBatch,
  editSemiProductSalesOrderDetail,
  deleteSemiProductSalesOrderDetail,
  getSemiProductSalesOrderDetailByMaterialId,
  calculateDetailSubtotal,
  
  // 交易记录API
  getSemiProductSalesOrderTransactionList,
  getSemiProductSalesOrderTransactionByOrderId,
  addSemiProductSalesOrderTransaction,
  editSemiProductSalesOrderTransaction,
  deleteSemiProductSalesOrderTransaction,
  processPayment,
  processRefund,
  calculatePaidAmount,
  calculatePrepaymentAmount,
  getSemiProductSalesOrderTransactionByType,
  
  // 进度记录API
  getSemiProductSalesOrderProgressList,
  getSemiProductSalesOrderProgressByOrderId,
  addSemiProductSalesOrderProgress,
  editSemiProductSalesOrderProgress,
  deleteSemiProductSalesOrderProgress,
  recordStatusChange,
  getSemiProductSalesOrderProgressByStatus,
  getSemiProductSalesOrderProgressByOperationType,
  getLatestProgress,
  batchAddProgress
}
